package org.foreverframework.mail;

import java.io.InputStream;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
import javax.mail.util.ByteArrayDataSource;

public class MailHandler {

	private String fromEmail;
	private String smtpHost;
	private String username;
	private String password;

	public MailHandler() {
	}

	public void init() {
		System.out.println("sendMailHtml");
		try {
			testSendMailHtml();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//test osgi init snedMail
	public void testSendMailHtml() throws Exception {
		Properties props = new Properties();
		props.put("mail.smtp.host", "smtp.qq.com");
		props.put("mail.from", "597466738@qq.com");
		props.put("mail.smtp.auth", true);
		Session session = Session.getInstance(props, new EmailAuthenticator("597466738",
				"biedaohao"));
		MimeMessage msg = new MimeMessage(session);
		msg.setFrom();
		msg.setRecipients(Message.RecipientType.TO, "396481925@qq.com");
		msg.setSubject("JavaMail hello world example");
		msg.setSentDate(new Date());

		Multipart multipart = new MimeMultipart();
		// add img
		InputStream in = getClass().getClassLoader().getResourceAsStream(
				"dog.jpg");
		byte[] bytes = new byte[in.available()];
		in.read(bytes);
		in.close();
		BodyPart imgPart = new MimeBodyPart();
		imgPart.setDataHandler(new DataHandler(new ByteArrayDataSource(bytes,
				"application/octet-stream")));
		imgPart.setFileName(MimeUtility.encodeText("a.jpg"));
		imgPart.setHeader("Content-ID", "<img_1>");
		multipart.addBodyPart(imgPart);

		// add html
		InputStream is = getClass().getClassLoader().getResourceAsStream(
				"table.html");
		bytes = new byte[is.available()];
		is.read(bytes);
		String content = new String(bytes, "UTF-8");
		//is.close();
		MimeBodyPart htmlPart = new MimeBodyPart();
		htmlPart.setContent(content, "text/html;charset=UTF-8");
		multipart.addBodyPart(htmlPart);

		msg.setContent(multipart);
		Transport.send(msg);
	}
	

	public Session buildSession() {
		Properties props = System.getProperties();
		props.put("mail.smtp.host", smtpHost);
		props.put("mail.smtp.auth", true);
		props.put("mail.debug", false);
		Session session = Session.getDefaultInstance(props,
				new EmailAuthenticator(username, password));
		return session;
	}

	public void sendMail(MailBean mailInfo, boolean html) throws Exception {
	}

	public String getFromEmail() {
		return fromEmail;
	}

	public void setFromEmail(String fromEmail) {
		this.fromEmail = fromEmail;
	}

	public String getSmtpHost() {
		return smtpHost;
	}

	public void setSmtpHost(String smtpHost) {
		this.smtpHost = smtpHost;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

}
